package com.hsh.util;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hsh.bean.BootStrapComment;
import com.hsh.bean.Comment;
import com.hsh.bean.ResultData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ToBootStrapUtil {

    public static List<BootStrapComment> toBootStrapList(List<Comment> comments) {
        List<BootStrapComment> pageData = new ArrayList<>();
        for(Comment comment: comments){
            pageData.add(ToBootStrapUtil.getBootStrapComment(comment));
        }
        return pageData;
    }

    public static String toJSON(PageInfo<Comment> commentPageInfo) {
        List<BootStrapComment> pageData = toBootStrapList(commentPageInfo.getList());

        //将查询结果封装到ResultData中显示
        ResultData<BootStrapComment> resultData = new ResultData<>();
        resultData.setTotal(commentPageInfo.getTotal());
        resultData.setRows(pageData);
        commentPageInfo.getList().forEach(System.out::println);
        //将结果封装成JSON格式
        return JSON.toJSONString(resultData);
    }

    public static BootStrapComment getBootStrapComment(Comment comment) {
        Integer cid =
                comment.getCid() == null ? null : comment.getCid();
        Integer uid = comment.getUid();
        String nickname = comment.getNickname();
        String cContent = comment.getCContent();
        Integer isFine = comment.getIsFine();
        String cDate =
                comment.getCDate() == null ? "-----" : DateTimeFormatUtil.format(comment.getCDate());
        BootStrapComment bootStrapComment = new BootStrapComment(cid, uid, nickname, cContent, isFine, cDate);
        return bootStrapComment;
    }

}
